//
//  BGBuyerListDetailController.m
//  bigMall
//
//  Created by LTOVE on 16/9/10.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGBuyerListDetailController.h"
#import "BGBuyerListDetaiCell.h"
#import "BGBuyerListDetailCellModel.h"
#import "BGDateResult.h"
#import "BGBuyerOrderDetailModel.h"
#import "BGFaBuZNXController.h"
#import "BGJinDuViewController.h"
#import "LTOVEDynamicCreakButton.h"
#import "BGGoodAttrsModel.h"

#import "BGDateResult.h"
#import "BGBuyerOrderStepParam.h"
#import "MJExtension.h"

#import "BGCommitViewController.h"

#import "BGAddReportViewController.h"

#import "BGSellerOrderStepModel.h"
#import "BGQCreportDataModel.h"
#import "BGCheckReportModel.h"
#import "BGBasicUrl.h"
#import "BGCheckReportModel.h"
//#import "BGCheckReportModel.h"
#import "BGMessageContentFrameModel.h"
//#import "store_pic_info.h"

#import "MBProgressHUD+MJ.h"
#import "MBProgressHUD.h"

@interface GWView : UIView

@end
@implementation GWView



@end
@interface BGBuyerListDetailController ()<dynamicButtonDelegate,BGCommitViewControllerDelegate>{
    
    NSMutableArray *dataArray;
    NSString *userName;
    NSString *listNum;
    NSString *_status;
    NSDictionary *param;
    NSObject *tempResult;
    NSArray *pay_scaleArray;
    CGFloat price;
//    NSString *orderId;
//    NSDictionary *resultData;
    NSString *exception_status;
    
}



@property (nonatomic,weak)GWView *views;
@property (nonatomic,weak)GWView *viewss;
@end

@implementation BGBuyerListDetailController
/**
 *  评价
 *
 *  @param content    content description
 *  @param scoreOne   scoreOne description
 *  @param scoreTwo   scoreTwo description
 *  @param scoreThree scoreThree description
 */
- (void)commitViewBtnClickWithContent:(NSString *)content scoreOne:(NSString *)scoreOne scoreTwo:(NSString *)scoreTwo scoreThree:(NSString *)scoreThree
{
    BGBuyerOrderStepParamComment * paramss = [[BGBuyerOrderStepParamComment alloc]init];
    paramss.orderId = _orderId;
    paramss.comment = content;
    paramss.score1 = scoreOne;
    paramss.score2 = scoreTwo;
    paramss.score3 = scoreThree;
    param = [paramss getParamDic];
    [self alertMessageWithMsgWithOutData:@"确认评价？"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buyerListDetailBtnClisk) name:@"buyerListDetail" object:nil];
//    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
//    [self.tableView.header beginRefreshing];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithTitle:@"进度" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarClick)];
    self.navigationItem.rightBarButtonItem = rightBar;
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passwordFinishedWith:) name:@"password" object:nil];
    self.edgesForExtendedLayout = UIRectEdgeNone;

}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
/**
 *  进度按钮
 */
- (void)rightBarClick
{
    BGJinDuViewController *vc = [[BGJinDuViewController alloc]init];
    vc.typeId = listNum;
    [self.navigationController pushViewController:vc animated:YES];
    
}
/**
 *  发送站内信的通知
 */
- (void)buyerListDetailBtnClisk
{
    BGFaBuZNXController *vc = [[BGFaBuZNXController alloc] init];
    vc.name = userName;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setOrderId:(NSString *)orderId
{
    GWLog(@"%@",orderId);
    _orderId = orderId;
    
    NSDictionary *dic = @{
                          @"action":@"getComment",
                          @"orderId":self.orderId
                          };
    [BGDateResult buyerOrderStepWithParamDic:dic success:^(NSObject *result) {
        BGSellerOrderStepModel *model = (BGSellerOrderStepModel *)result;
        //        NSLog(@"%@",model.content);
        self.comStr = model.content;
        self.oneStr = model.score_1;
        self.twoStr = model.score_2;
        self.threeStr = model.score_3;
        //
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
    [self loadData];
}



- (void)loadData
{
    [MBProgressHUD hideHUD];
//    [MBProgressHUD showMessage:@"加载中..."];
    if (!self.isSeller) {
        [BGDateResult buyerOrderDetailWithOrderId:self.orderId success:^(NSObject *result) {
            BGBasicresModel *resu = (BGBasicresModel *)result;
            //        resu = result;
            [MBProgressHUD hideHUD];
            BGBuyerOrderDetailModel *model = [BGBuyerOrderDetailModel new];
            model.status = resu.status;
            model.msg = resu.msg;
            BGBuyerOrderDetailResModel *res = [BGBuyerOrderDetailResModel objectWithKeyValues:resu.res];
            BGBuyerOrderDetailGoodsInfo *goodinfo = [BGBuyerOrderDetailGoodsInfo objectWithKeyValues:resu.res[@"goods_info"]];
            res.goods_info = goodinfo;
            BGBuyerOrderDetailUserInfoModel *userinfo = [BGBuyerOrderDetailUserInfoModel objectWithKeyValues:resu.res[@"user_info"]];
            BGBuyerOrderDetailShopInfo *shopInfo = [BGBuyerOrderDetailShopInfo objectWithKeyValues:resu.res[@"shop_info"]];
            res.shop_info = shopInfo;
            res.user_info = userinfo;
            model.res = res;
            
            
            _resultData = resu.keyValues;
            [self analysisDataWith:model andFirst:YES];
            
        } failure:^(NSError *error) {
            
            [MBProgressHUD hideHUD];
        }];
    }
    
}
- (void)addDynamicBtnWith:(NSArray *)titleArray
{
    NSDictionary *dic = @{
                          NSFontAttributeName:Font34,
                          NSForegroundColorAttributeName:[UIColor whiteColor]
                          };
    //    NSLog(@"%@",_btnArray);
    CGFloat x = BGButtonCellHeigh;
    CGFloat heigh = screenH - x;
    LTOVEDynamicCreakButton *view = [LTOVEDynamicCreakButton buttonViewWithFrame:CGRectMake(0, heigh, screenW, BGButtonCellHeigh) titleDataArray:titleArray andCol:(int)titleArray.count andEdgInset:UIEdgeInsetsMake(10,BGMaginLeft , 10, BGMaginLeft) andMaginX:10 andMaginY:0 andBtnHeigh:BGMessageBtn andBtnNorColor:read_ andBtnHeighColor:read_ andRadius:7 andFont:dic];
    //    LTOVEDynamicCreakButton *view = [LTOVEDynamicCreakButton buttonViewWithFrame:CGRectMake(0,heigh, screenW, BGButtonCellHeigh) titleDataArray:self.btnArray andEdgInset:UIEdgeInsetsMake(10, BGMaginLeft, 10, BGMaginLeft) andMaginX:0 andMaginY:0 andBtnHeigh:BGMessageBtn andBtnNorColor:read_ andBtnHeighColor:read_ andRadius:7 andFont:dic];
    //    view.delegate = self;
    //    view.y = 300;
    view.backgroundColor = lightwhite_;
    view.delegate = self;
    view.backgroundColor = [UIColor clearColor];
//    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, x, 0);
//    self.tableView.contentOffset = CGPointMake(0, -64);
    if ([exception_status isEqualToString:@"4"]) {
        LTOVEDynamicCreakButton *views = [[LTOVEDynamicCreakButton alloc]initWithFrame:view.frame];
//        view = nil;
        views.backgroundColor = lightwhite_;
//        view = views;
        UILabel *label = [UILabel new];
        label.text = @"仲裁中";
        label.textAlignment = NSTextAlignmentCenter;
        label.layer.cornerRadius = 7;
        label.layer.masksToBounds = YES;
        label.width = view.width - BGMaginLeft * 2;
        label.height = view.height - 20;
        label.backgroundColor = lightGray_;
        label.textColor = lightwhite_;
        //    [label sizeToFit];
        label.center = CGPointMake(view.width * 0.5, view.height * 0.5);
        [views addSubview:label];
        view.backgroundColor = lightwhite_;
//        self.viewss = views;
//        [self.viewss removeFromSuperview];
        [BGKeyWindow addSubview:views];
    }else{
//        self.views = view;
//        [self.views removeFromSuperview];
        view.backgroundColor = lightwhite_;
        [BGKeyWindow addSubview:view];
    }
}
/**
 *  btn点击代理方法
 *
 *  @param btn btn description
 */
- (void)dynamicButtonClick:(UIButton *)btn
{
    GWLog(@"%ld",(long)btn.tag);
    NSLog(@"%@",_status);
    switch ([_status integerValue]) {
        case 0:
            //等待买家付款     --->取消订单
            [self cancelList];
            break;
        case 1:
            // 付首款         --->取消订单,支付首付
            switch (btn.tag) {
                case 0:
                    //取消订单
                    [self cancelList];
                    break;
                case 1:
                    //支付首付
                    [self payOneStep];
                    break;
                default:
                    break;
            }
            break;
        case 2:
            //等待商家备货    --->申请仲裁
            [self arbitration];
            break;
        case 3:
            //付中期款        --->申请仲裁，付中期款
            switch (btn.tag) {
                case 0:
                    //申请仲裁
                    [self arbitration];
                    break;
                case 1:
                    //付中期款
                    [self payTwoStep];
                    break;
                default:
                    break;
            }
            break;
        case 4:
            //验收提货        --->申请仲裁，提交验收报告
            switch (btn.tag) {
                case 0:
                    //申请仲裁
                    GWLog(@"申请仲裁");
                    [self arbitration];
                    break;
                case 1:
                    //提交验收报告
                    GWLog(@"提交验收报告");
                    [self CommitReport];
                    break;
                default:
                    break;
            }
            break;
        case 5:
            //验收发票        --->申请仲裁，拒绝确认发票，确认发票
            switch (btn.tag) {
                case 0:
                    //申请仲裁
                    GWLog(@"申请仲裁");
                    [self arbitration];
                    break;
                case 1:
                    //拒绝确认发票
                    GWLog(@"确认发票");
                    [self refuseSureFaPiao];
                    break;
//                case 2:
//                    //确认发票
//                    GWLog(@"确认发票");
//                    [self sureFaPiao];
//                    break;
                default:
                    break;
            }
            break;
        case 6:
            //付尾款         --->申请仲裁，付尾款
            switch (btn.tag) {
                case 0:
                    //申请仲裁
                    [self arbitration];
                    break;
                case 1:
                    //付尾款
                    [self payThreeStep];
                    break;
                default:
                    break;
            }
            break;
        case 7:
            //评价          --->评价
            [self evaluate];
            break;
        case 8:
            //已完成         --->我的评价
            [self Myevaluate];
            break;
            
        default:
            break;
    }
}

- (void)btnClickThenCommit
{
//    NSObject *temp;
    GWLog(@"%@",param);
    [BGDateResult buyerOrderStepWithParamDic:param success:^(NSObject *result) {
//        return result;
        BGBasicResult *model = (BGBasicResult *)result;
        [self alertMessageWithMsgWithOutDatassss:model.msg action:param[@"action"] status:[model.status intValue]];
            tempResult = result;
//        NSObject *temp = result;
    } failure:^(NSError *error) {
//        return error;
        tempResult = error;
    }];
}

- (void)alertMessageWithMsgWithOutDatassss:(NSString *)msg action:(NSString *)actions status:(int)status
{
    UIAlertController *aletrVC = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
//        [self btnClickThenCommit];
        if (status != 1) {
            return ;
        }
        if ([actions isEqualToString:@"cancel"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }else if ([actions isEqualToString:@"payOne"]){
            //首付
            
            int statusInt = [_status intValue];
            _status = [NSString stringWithFormat:@"%d",statusInt + 1];
//            BGBuyerOrderDetailModel *model = [BGBuyerOrderDetailModel objectWithKeyValues:resultData];
//            NSLog(@"%@------->",model.keyValues);
            [self.views removeFromSuperview];
//            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
            [self analysisDataWith:[BGBuyerOrderDetailModel objectWithKeyValues:_resultData] andFirst:NO];
//            [self addBtnWithStatus:_status];
            
        }else if ([actions isEqualToString:@"payTwo"]){
            //首付
            int statusInt = [_status intValue];
            _status = [NSString stringWithFormat:@"%d",statusInt + 1];
            [self analysisDataWith:[BGBuyerOrderDetailModel objectWithKeyValues:_resultData] andFirst:NO];
            [self.views removeFromSuperview];
//            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
            [self addBtnWithStatus:_status];
        }else if ([actions isEqualToString:@"payThree"]){
            //首付
            int statusInt = [_status intValue];
            _status = [NSString stringWithFormat:@"%d",statusInt + 1];
            [self analysisDataWith:[BGBuyerOrderDetailModel objectWithKeyValues:_resultData] andFirst:NO];
            [self.views removeFromSuperview];
//            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
            [self addBtnWithStatus:_status];
        }else if ([actions isEqualToString:@"arbitration"]){
            //申请仲裁
            exception_status = @"4";
            
            [self.views removeFromSuperview];
            [self analysisDataWith:[BGBuyerOrderDetailModel objectWithKeyValues:_resultData] andFirst:NO];
            //            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
//            [self addBtnWithStatus:_status];
#warning 这里是仲裁的结果
        }else if ([actions isEqualToString:@"comment"]){
            //首付
            int statusInt = [_status intValue];
            _status = [NSString stringWithFormat:@"%d",statusInt + 1];
            [self analysisDataWith:[BGBuyerOrderDetailModel objectWithKeyValues:_resultData] andFirst:NO];
            [self.views removeFromSuperview];
            //            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
            [self addBtnWithStatus:_status];
        }
        
    }];
    [aletrVC addAction:cancel];
    [aletrVC addAction:sure];
    [self presentViewController:aletrVC animated:YES completion:^{
        
        
    }];
}

/**
 *  弹窗
 *
 *  @param msg msg description
 */
- (void)alertMessageWithMsgWithOutData:(NSString *)msg
{
    UIAlertController *aletrVC = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self btnClickThenCommit];
        
    }];
    [aletrVC addAction:cancel];
    [aletrVC addAction:sure];
    [self presentViewController:aletrVC animated:YES completion:^{
        
        
    }];
}
/**
 *  带输入框的弹窗 付款专用
 *
 *  @param msg msg description
 */
- (void)alertViewWithInputAndMsg:(NSString *)msg andInputMsg:(NSString *)inputMsg class:(NSObject *)class isPassword:(BOOL)ispassword
{
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        textField.placeholder = inputMsg;
        textField.secureTextEntry = ispassword;
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *passWord = alertVC.textFields.firstObject.text;
        if (ispassword) {
            if ([class isKindOfClass:[BGBuyerOrderStepParamPayOne class]]) {
                BGBuyerOrderStepParamPayOne *params = (BGBuyerOrderStepParamPayOne *)class;
                params.payPassword = passWord;
                param = [params getParamDic];
            }else if ([class isKindOfClass:[BGBuyerOrderStepParamPayTwo class]]){
                BGBuyerOrderStepParamPayTwo *twoParam = (BGBuyerOrderStepParamPayTwo *)class;
                twoParam.payPassword = passWord;
                param = [twoParam getParamDic];
            }else if ([class isKindOfClass:[BGBuyerOrderStepParamPayThree class]]){
                BGBuyerOrderStepParamPayThree *threeParam = (BGBuyerOrderStepParamPayThree *)class;
                threeParam.payPassword = passWord;
                param = [threeParam getParamDic];
            }
        }else{
            //仲裁
            BGBuyerOrderStepParamArbitration *pass = (BGBuyerOrderStepParamArbitration *)class;
            pass.reason = passWord;
            param = [pass getParamDic];
        }
        
        
        
        [self btnClickThenCommit];
        
    }];
    [alertVC addAction:cancel];
    [alertVC addAction:sure];
    [self presentViewController:alertVC animated:YES completion:^{
        
        
    }];
}

- (void)passwordFinishedWith:(NSNotification *)notify
{
    NSString *password = notify.userInfo[@"key"];
    
}

#pragma mark 按钮操作
/**
 *  取消订单
 */
- (void)cancelList
{
   BGBuyerOrderStepParamCancel * paramss = [[BGBuyerOrderStepParamCancel alloc]init];
    paramss.orderId = _orderId;
    param = [paramss getParamDic];
    [self alertMessageWithMsgWithOutData:@"确认取消订单"];
}
/**
 *  支付首款
 */
- (void)payOneStep
{
   
    BGBuyerOrderStepParamPayOne * paramss = [[BGBuyerOrderStepParamPayOne alloc]init];
//    paramss.buyerFactorage
    paramss.orderId = _orderId;
    CGFloat oneScale = [pay_scaleArray[2] floatValue];
    paramss.payMoney = [NSString stringWithFormat:@"%f",oneScale * price];
//    [self alertViewWithInputAndMsg:@"请输入支付密码" andInputMsg:@"支付密码" class:paramss ispassword:YES];
    [self alertViewWithInputAndMsg:@"请输入支付密码" andInputMsg:@"支付密码" class:paramss isPassword:YES];
}
/**
 *  申请仲裁
 */
- (void)arbitration
{
    BGBuyerOrderStepParamArbitration * paramss = [[BGBuyerOrderStepParamArbitration alloc]init];
    //    paramss.buyerFactorage
    paramss.action = @"arbitration";
    paramss.orderId = _orderId;
//    param = [paramss getParamDic];
//    [self btnClickThenCommit];
    [self alertViewWithInputAndMsg:@"请输入仲裁理由" andInputMsg:@"仲裁理由" class:paramss isPassword:NO];
}
/**
 *  付中期款
 */
- (void)payTwoStep
{
    BGBuyerOrderStepParamPayTwo * paramss = [[BGBuyerOrderStepParamPayTwo alloc]init];
    //    paramss.buyerFactorage
    paramss.orderId = _orderId;
    CGFloat oneScale = [pay_scaleArray[1] floatValue];
    paramss.payMoney = [NSString stringWithFormat:@"%f",oneScale * price];
//    [self alertViewWithInputAndMsg:@"请输入支付密码" andInputMsg:@"支付密码" class:paramss ispassword:YES];
    [self alertViewWithInputAndMsg:@"请输入支付密码" andInputMsg:@"支付密码" class:paramss isPassword:YES];
}
/**
 *  付尾款
 */
- (void)payThreeStep
{
    BGBuyerOrderStepParamPayThree * paramss = [[BGBuyerOrderStepParamPayThree alloc]init];
    //    paramss.buyerFactorage
    paramss.orderId = _orderId;
    param = [paramss getParamDic];
    [self btnClickThenCommit];
}
/**
 *  提交验收报告
 */
- (void)CommitReport
{
//    BGBuyerOrderStepParamPostReport * paramss = [[BGBuyerOrderStepParamPostReport alloc]init];
//    //    paramss.buyerFactorage
//    paramss.orderId = _orderId;
//    param = [paramss getParamDic];
//    [self btnClickThenCommit];
    
    BGAddReportViewController *VC = [BGAddReportViewController new];
    VC.orderId = _orderId;
    VC.title = @"提交验收报告";
    UIBarButtonItem *bac = [UIBarButtonItem new];
    bac.title = @"";
    self.navigationItem.backBarButtonItem = bac;
    [self.navigationController pushViewController:VC animated:YES];
    
}
/**
 *  确认发票
 */
- (void)sureFaPiao
{
    BGBuyerOrderStepParamReceive * paramss = [[BGBuyerOrderStepParamReceive alloc]init];
    //    paramss.buyerFactorage
    paramss.orderId = _orderId;
    param = [paramss getParamDic];
    [self btnClickThenCommit];

}
/**
 *  拒绝发票
 */
- (void)refuseSureFaPiao
{
//    BGBuyerOrderStepParamReceive * paramss = [[BGBuyerOrderStepParamReceive alloc]init];
//    //    paramss.buyerFactorage
//    param = [paramss getParamDic];
//    [self btnClickThenCommit];
}
/**
 *  评价
 */
- (void)evaluate
{
//    BGBuyerOrderStepParamArbitration * paramss = [[BGBuyerOrderStepParamArbitration alloc]init];
//    //    paramss.buyerFactorage
//    param = [paramss getParamDic];
//    [self btnClickThenCommit];
    BGCommitViewController *vc = [[BGCommitViewController alloc]init];
//    BGCommitViewController *vc = [BGCommitViewController initControlWithCommitText:@"sssssssss" anddsorceValueOne:@"4" sorceTwo:@"2" scoreThree:@"0"];
    vc.writeEnabel = YES;
    vc.delegate = self;
    vc.title = @"发表评价";
    [self.navigationController pushViewController:vc animated:YES];
}
/**
 *  我的评价
 */
- (void)Myevaluate
{
        BGCommitViewController *vc = [BGCommitViewController initControlWithCommitText:self.comStr anddsorceValueOne:self.oneStr sorceTwo:self.twoStr scoreThree:self.threeStr];
//    vc.writeEnabel = YES;
//    vc.delegate = self;
    vc.title = @"我的评价";
    [self.navigationController pushViewController:vc animated:YES];
//    BGBuyerOrderStepParamArbitration * paramss = [[BGBuyerOrderStepParamArbitration alloc]init];
//    //    paramss.buyerFactorage
//    param = [paramss getParamDic];
//    [self btnClickThenCommit];
}

#pragma mark 按钮操作结束
- (void)viewWillDisappear:(BOOL)animated
{

    for (LTOVEDynamicCreakButton *view in BGKeyWindow.subviews) {
        if ([view isKindOfClass:[LTOVEDynamicCreakButton class]]) {
            [view removeFromSuperview];
        }
    }
//    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
- (void)viewWillAppear:(BOOL)animated
{
    GWLog(@"%f------------",self.tableView.contentOffset.y);
//    self.tableView.contentOffset = CGPointMake(0, -64);
    if (self.orderId && !self.isSeller) {
        [self loadData];
    }
    if (!self.views && _status) {
        [self addBtnWithStatus:_status];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    GWLog(@"%f------------",self.tableView.contentOffset.y);
//    self.tableView.contentOffset = CGPointMake(0, -64);
}
- (void)addBtnWithStatus:(NSString *)status
{
//    NSString *statuss = status;
    
    _status = [NSString stringWithFormat:@"%@",status];
    
//    status = @"4";
    switch ([status integerValue]) {
        case 0:
            //等待买家付款     --->取消订单
            [self addDynamicBtnWith:@[@"取消订单"]];
            break;
        case 1:
            // 付首款         --->取消订单,支付首付
            [self addDynamicBtnWith:@[@"取消订单",@"支付首付"]];
            break;
        case 2:
            //等待商家备货    --->申请仲裁
            [self addDynamicBtnWith:@[@"申请仲裁"]];
            break;
        case 3:
            //付中期款        --->申请仲裁，付中期款
            [self addDynamicBtnWith:@[@"申请仲裁",@"付中期款"]];
            break;
        case 4:
            //验收提货        --->申请仲裁，提交验收报告
            [self addDynamicBtnWith:@[@"申请仲裁",@"提交验收报告"]];
            break;
        case 5:
            //验收发票        --->申请仲裁，拒绝确认发票，确认发票
            [self addDynamicBtnWith:@[@"申请仲裁",@"确认发票"]];
            break;
        case 6:
            //付尾款         --->申请仲裁，付尾款
            [self addDynamicBtnWith:@[@"申请仲裁",@"付尾款"]];
            break;
        case 7:
            //评价          --->评价
            [self addDynamicBtnWith:@[@"评价"]];
            break;
        case 8:
            //已完成         --->我的评价
            [self addDynamicBtnWith:@[@"我的评价"]];
            break;
            
        default:
            break;
    }
    

}

- (void)analysisDataWith:(NSObject *)result andFirst:(BOOL)isFirst
{
    dataArray = [NSMutableArray array];
    BGBuyerOrderDetailModel *model = (BGBuyerOrderDetailModel *)result;
    BGBuyerOrderDetailResModel *resModel = model.res;
//    BGBuyerOrderDetailUserInfoModel *userInfoModel = resModel.user_info;
    BGBuyerOrderDetailShopInfo *userInfoModel = resModel.shop_info;
    
    BGBuyerOrderDetailCheckReportModel *checkReportModel = (BGBuyerOrderDetailCheckReportModel *)resModel.check_report;
//    NSLog(@"%@",userInfoModel.keyValues);
    BGBuyerOrderDetailGoodsInfo *goodInfoModel = resModel.goods_info;
//    resModel.status = @"4";
    resModel.status = isFirst ? resModel.status : _status;
    if (isFirst) {
        exception_status = resModel.exception_status;
    }else{
        resModel.exception_status = exception_status;
    }
//    GWLog(@"%@",resModel.status);
    _orderId = resModel.idStr;
    NSString *status = resModel.status;
    pay_scaleArray = goodInfoModel.pay_scaleArray;
    price = [goodInfoModel.price floatValue];
    [self addBtnWithStatus:status];
    
    //订单编号
    BGBuyerListDetailCellNumModel *listNumModel = [BGBuyerListDetailCellNumModel new];
    listNumModel.listNumStr = resModel.idStr;
    listNumModel.listNumSecStr = resModel.flow_name;
    listNumModel.height = 45;
    listNum = resModel.status;
    [dataArray addObject:listNumModel];
    
    //用户信息
    BGBuyerListDetailCellUserInfoModel *userInfoModels = [BGBuyerListDetailCellUserInfoModel new];
    userInfoModels.userName = userInfoModel.name;
    userInfoModels.userPhoneNum = userInfoModel.phone;
    userInfoModels.comName = userInfoModel.com_name;
    userInfoModels.height = BGMagListMaginY * 3 + 30;
    userName = userInfoModel.name;
    [dataArray addObject:userInfoModels];
    
    //基本信息
    BGBuyerListDetailCellTitleModel *titleModel = [BGBuyerListDetailCellTitleModel new];
    titleModel.titleStr = @"基本信息";
    titleModel.height = 45;
    [dataArray addObject:titleModel];
    
        //动态列表
    BGBuyerListDetailCellDynamicDataModel *basicInfoDynamicListModel = [BGBuyerListDetailCellDynamicDataModel new];
    basicInfoDynamicListModel.dynamicDataArray = [goodInfoModel getBasicInfoArray];
    basicInfoDynamicListModel.height = ((basicInfoDynamicListModel.dynamicDataArray.count + 1) / 2) * 15 + ((basicInfoDynamicListModel.dynamicDataArray.count + 1) / 2 + 1) * BGMagListMaginY;
    [dataArray addObject:basicInfoDynamicListModel];
    
        //仓单图片
    NSLog(@"%@",goodInfoModel.store_pic_info);
    if (goodInfoModel.store_pic_info) {
        NSLog(@"%@----------",goodInfoModel.check_report_info);
        BGBuyerListDetailCellCDPicModel *QcModel = [[BGBuyerListDetailCellCDPicModel alloc]init];
        QcModel.title = @"仓单图片";
        NSMutableArray *QcDataArray = [NSMutableArray new];
        for (store_pic_info *UrlModel in goodInfoModel.store_pic_info) {
            NSString *basicUrl = [BGBasicUrl getBasicUrl];
            
            GWLog(@"%@",goodInfoModel.store_pic_info);
            NSString *url = [[BGBasicUrl getBasicUrl] stringByAppendingString:UrlModel.url];//拼接图片URL
            //        NSString *url = @"s";
            GWLog(@"%@",url);
            [QcDataArray addObject:url];
        }
        QcModel.imageDataArray = QcDataArray;

        //    计算行高
//        QcModel.height = QcModel.imageDataArray.count ? BGTitleHeigh + ((QcModel.imageDataArray.count + 1) /2 ) * BGImgListHeigh + ((QcModel.imageDataArray.count + 1) /2 +1) * BGMagListMaginY :BGTitleHeigh;
        QcModel.height = 143 * (QcDataArray.count / 3 + 1) + 30;
//        NSLog(@"%@----------",QcFmodel.keyValues);
        [dataArray addObject:QcModel];
      
    }
    
    
#warning 没有找到对应的属性
    //分割线
    BGBuyerListDetailCellLineModel *oneLineModel = [BGBuyerListDetailCellLineModel new];
    oneLineModel.height = 10;
    [dataArray addObject:oneLineModel];
    //附加信息
//    BGBuyerListDetailCellTitleModel *addInfoTitleModel = [BGBuyerListDetailCellTitleModel new];
//    addInfoTitleModel.titleStr = @"附加信息";
//    addInfoTitleModel.height = 45;
//        //动态列表
//    BGBuyerListDetailCellDynamicDataModel *addInfoDynamicModel = [BGBuyerListDetailCellDynamicDataModel new];
//    addInfoDynamicModel.dynamicDataArray = [self getProductInfoArrayWithGoodArray:goodInfoModel.goods_attrs];
//    addInfoDynamicModel.height = ((addInfoDynamicModel.dynamicDataArray .count + 1) / 2) * 15 + ((addInfoDynamicModel.dynamicDataArray.count  + 1) / 2 + 1) * BGMagListMaginY;
    //分割线
    BGBuyerListDetailCellLineModel *twoLineModel = [BGBuyerListDetailCellLineModel new];
    twoLineModel.height = 10;
    //检验报告
    if (goodInfoModel.check_report_info.count > 0) {
        BGBuyerListDetailCellTitleModel *reportTitle = [BGBuyerListDetailCellTitleModel new];
        reportTitle.titleStr = @"检验报告";
        reportTitle.height = 45;
        [dataArray addObject: reportTitle];
        //图片
        BGBuyerListDetailCellPicModel *imagesModel = [BGBuyerListDetailCellPicModel new];
        NSMutableArray *QcDataArray = [NSMutableArray new];
        for (store_pic_info *UrlModel in goodInfoModel.check_report_info) {
//            NSString *basicUrl = [BGBasicUrl getBasicUrl];
            
            GWLog(@"%@",goodInfoModel.store_pic_info);
            NSString *url = [[BGBasicUrl getBasicUrl] stringByAppendingString:UrlModel.url];//拼接图片URL
            //        NSString *url = @"s";
            GWLog(@"%@",url);
            [QcDataArray addObject:url];
        }
        imagesModel.imageDataArray = QcDataArray;
        imagesModel.height  = 143 * (QcDataArray.count / 3 + 1) + 10;
        //    NSLog(@"````%@",goodInfoModel.check_report_info);
        [dataArray addObject:imagesModel];

    }
    
    
    
    //验收报告
    
    if (checkReportModel) {
        BGBuyerListDetailCellTitleModel *reviveReportTitle = [BGBuyerListDetailCellTitleModel new];
        reviveReportTitle.titleStr = @"验收报告";
        reviveReportTitle.height = 45;
        [dataArray addObject:reviveReportTitle];
        //动态列表
        BGBuyerListDetailCellDynamicDataModel *reportDynamicModel = [BGBuyerListDetailCellDynamicDataModel new];

        NSDictionary *dic = checkReportModel.keyValues;
//        BGBuyerOrderDetailCheckReportModel *modelsss = [BGBuyerOrderDetailCheckReportModel objectWithKeyValues:dic];file_paths
//        NSLog(@">>>>>>%@",dic[@"final_number"]);
//        NSLog(@">>>>>>%@",dic[@"final_price"]);
//        NSLog(@">>>>>>%@",dic[@"freight"]);
//        NSLog(@">>>>>>%@",dic[@"file_paths"]);
        NSArray *imagearray = dic[@"file_paths"];
                NSArray *array = @[
        
                                   [self getDictWithTitle:@"收货重量" andValue:dic[@"final_number"]],
                                   [self getDictWithTitle:@"运费价格" andValue:dic[@"final_price"]],
                                   [self getDictWithTitle:@"最终成交价(总价)" andValue:dic[@"freight"]],
                                   ];
        reportDynamicModel.dynamicDataArray = array;
        reportDynamicModel.height = 50;
//        reportDynamicModel.height = 100;
        [dataArray addObject:reportDynamicModel];
        if (imagearray.count > 0) {
            NSLog(@"%@----------",goodInfoModel.check_report_info);
            BGBuyerListDetailCellCDPicModel *QcModel = [[BGBuyerListDetailCellCDPicModel alloc]init];
            QcModel.title = @"验收报告";
            NSMutableArray *QcDataArray = [NSMutableArray new];
            for (NSString *UrlModel in imagearray) {
//                NSString *basicUrl = [BGBasicUrl getBasicUrl];
                
                GWLog(@"%@",goodInfoModel.store_pic_info);
                NSString *url = [[BGBasicUrl getBasicUrl] stringByAppendingString:UrlModel];//拼接图片URL
                //        NSString *url = @"s";
                GWLog(@"%@",url);
                [QcDataArray addObject:url];
            }
            QcModel.imageDataArray = QcDataArray;
            
            //    计算行高
//            QcModel.height = QcModel.imageDataArray.count ? BGTitleHeigh + ((QcModel.imageDataArray.count + 1) /2 ) * BGImgListHeigh + ((QcModel.imageDataArray.count + 1) /2 +1) * BGMagListMaginY :BGTitleHeigh;
            QcModel.height = 143 * (imagearray.count / 3 + 1) + 20;
            //        NSLog(@"%@----------",QcFmodel.keyValues);
            [dataArray addObject:QcModel];
            
        }
    }
   
    //报告图片
#warning 没有找到报告的图片属性
    
//    dataArray = [NSMutableArray arrayWithObjects:listNumModel,userInfoModels,titleModel,basicInfoDynamicListModel,oneLineModel,addInfoTitleModel,addInfoDynamicModel,twoLineModel,reportTitle,reviveReportTitle, reportDynamicModel,nil];
//    dataArray = [NSMutableArray arrayWithObjects:listNumModel,userInfoModels,titleModel,basicInfoDynamicListModel,oneLineModel,twoLineModel,reportTitle,reviveReportTitle, reportDynamicModel,nil];
//    self.tableView.contentOffset = CGPointMake(0, 0);
    [self.tableView reloadData];
}


- (NSArray *)getProductInfoArrayWithGoodArray:(NSArray *)array
{
    NSMutableArray *productInfoArray = [NSMutableArray new];
    //    NSArray *a = _goods_attrs;
    for (BGGoodAttrsModel *m in array) {
        if (![m.attr_type  isEqual: @"3"] && ![m.attr_type  isEqual: @"5"]) {
            NSString * str = [m.default_value firstObject];
            [productInfoArray addObject:[self getDictWithTitle:m.name andValue:str]];
        }
        
    }
    
    return productInfoArray;
}
- (NSDictionary *)getDictWithTitle:(NSString *)title andValue:(NSString *)value
{
    if (!title) {
        title = @"";
    }
    if (!value) {
        value = @"";
    }
    return @{
             @"title":title,
             @"value":value
             };
//    return [NSString stringWithFormat:@"%@:%@",title,value];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BGBuyerListDetaiCell *cell = [BGBuyerListDetaiCell cellWithTableView:tableView andIndexPath:indexPath];
    BGBuyerListDetailCellModel *model = dataArray[indexPath.row];
    model.indexPath = indexPath;
    model.listNumSecStr = self.strs;
//    if (indexPath.row == 1) {
////        cell.backgroundColor = do;
//        cell.contentView.backgroundColor = read_;
//        
//    }
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BGBuyerListDetailCellModel *model = dataArray[indexPath.row];
    return model.height;
}
@end
